package de.fhaugsburg.games.boardgames;

import java.util.Set;

public class GridPoint implements Comparable<GridPoint> {

	public final int row;
	public final int col;

	public GridPoint(int row, int col) {
		this.row = row;
		this.col = col;
	}

	/**
	 * @return the String representation of a GridPoint in the form (row,col)
	 */
	public String toString() {
		return "(" + row + "," + col + ")";
	}
/**
 * duplicates a GridPoint
 */
	public Object clone() {
		return new GridPoint(row, col);
	}
/**
 * negates the row and the column value.
 * @return a new GridPoint with negated row and column value
 */
	public GridPoint neg() {
		return new GridPoint(-row, -col);
	}
/**
 * adds the row and column values from the current GridPoint to the delta GridPoint and return it as a new GridPoint.
 * @param delta
 * @return
 */
	public GridPoint plus(GridPoint delta) {
		return new GridPoint(row + delta.row, col + delta.col);
	}
/**
 * subtract from the current GridPoint the row and column values from the delta GridPoint and return it as a new GridPoint.	
 * @param delta
 * @return
 */
	public GridPoint minus(GridPoint delta){
		return new GridPoint(row - delta.row, col - delta.col);
	}
/**
 * checks if the given Set of GridPoints is collinear with the current GridPoint.	
 * @param points
 * @return
 */
	public boolean isManhattanColinearWith(Set<GridPoint> points){
		boolean isRowColinear = false;
		boolean isColColinear = false;
		for (GridPoint gridPoint : points) {
			if(isRowColinear){
				if(gridPoint.row != row)
					return false;
			}
			else if(isColColinear){
				if(gridPoint.col != col)
					return false;
			}
			else if(gridPoint.row == row)
				isRowColinear = true;
			else if(gridPoint.col == col)
				isColColinear = true;
				
		}
		if(isColColinear || isRowColinear)
			return true;
		else return false;
	}

	public boolean equals(Object o ){
		if(o instanceof GridPoint){
			GridPoint p = (GridPoint)o;
			if(row == p.row && col == p.col)
				return true;
			else return false;			
		}else return false;
			
	}

	@Override
	/**
	 * @return 1: GridPoint rc is left the current GridPoint
	 * @return -1: GridPoint rc is below  the current GridPoint
	 * @return 0: no analogy
	 */
	public int compareTo(GridPoint rc) {
		if(rc.col < col && rc.row == row)
			return 1; //
		if(rc.row > row && rc.col == col)
			return -1;//
		return 0;
	}
	

}
